import Vue from 'vue'
import VueRouter from 'vue-router'
// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'index',
    component: () => import('@/views/layout/index'),
    redirect: '/home',
    children: [{
        path: '/home',
        name: 'home',
        component: () => import('@/views/Home/home'),
        meta: {
          title: '首页'
        }
      },
      {
        path: '/gwcx',
        name: 'gwcx',
        component: () => import('@/views/Activity/Gwcx/gwcx'),
        meta: {
          title: '岗位查询'
        }
      },
      {
        path: '/gwcx/hdAddEdit',
        name: 'hdAddEdit',
        component: () => import('@/views/Activity/Gwcx/hdAddEdit'),
        meta: {
          title: '新增活动'
        }
      },
      {
        path: '/gwcx/hdAddEdit/:id',
        name: 'edit',
        component: () => import('@/views/Activity/Gwcx/hdAddEdit'),
        props: true,
        meta: {
          title: '编辑活动'
        }
      },
      {
        path: '/gwcx/basic/:id',
        name: 'basic',
        component: () => import('@/views/Activity/Gwcx/basic'),
        props: true,
        meta: {
          title: '活动详情'
        }
      }, {
        path: '/user',
        name: 'user',
        component: () => import('@/views/Activity/User/user'),
        meta: {
          title: '用户管理'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/login'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/register_create',
    component: () => import('@/views/Register/register'),
    meta: {
      title: '注册'
    }
  }, {
    path: '/404',
    name: 'NOT_FOUND',
    component: () => import('@/views/404/404'),
    meta: {
      title: '404'
    }
  }
]


const router = new VueRouter({
  routes,
  mode: 'history',
  base: '/xcxAdmin/'
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (localStorage.token) {
    if (to.path == '/login' || to.path == '/register_create') {
      next('/home')
    } else {
      next()
    }
  } else {
    if (to.path == '/login' || to.path == '/register_create') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
